let $ = require("./jquery.js");

let util = {
    request(url,params,type = "GET",success = ()=>{return}){
        let _pre = "http://api.wechat.uat.rockcent.com/api/v2/";
        $.ajax({
            url: _pre + url,
            type: type,
            data: params,
            beforeSend: function(request) {
                request.setRequestHeader("userToken",window.app.info.userToken || null);
                request.setRequestHeader("domain",window.app.info.domain || null);
            },
            success: function (res) {
                success(res)
            }
        })
    },

    getCookie(name) {
        let start = document.cookie.indexOf( name + "=" );
        if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
            return null;
        }
        if ( start == -1 ) return null;
        let len = start + name.length + 1;
        let end = document.cookie.indexOf( ';', len );
        if ( end == -1 ) end = document.cookie.length;
        return unescape( document.cookie.substring( len, end ) );
    },

    setCookie(name, value, expires, domain, secure) {
        let today = new Date();
        today.setTime( today.getTime() );
        if ( expires ) {
            expires = expires * 1000 * 60 * 60 * 24;
        }
        let expires_date = new Date( today.getTime() + (expires) );
        document.cookie = name+'='+escape( value ) +
            ( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
            ( ( domain ) ? ';domain=' + domain : '' ) +
            ( ( secure ) ? ';secure' : '' );
    },

    delCookie(name) {
        let date = new Date();
        date.setTime(date.getTime() - 10000);
        document.cookie = name + "=a; expires=" + date.toGMTString();
    },
}

module.exports = util;